package com.shanjupay.merchant.controller;

import com.shanjupay.common.domain.BusinessException;
import com.shanjupay.common.domain.CommonErrorCode;
import com.shanjupay.merchant.common.util.SecurityUtil;
import com.shanjupay.transaction.api.PayChannelService;
import com.shanjupay.transaction.api.dto.PayChannelDTO;
import com.shanjupay.transaction.api.dto.PayChannelParamDTO;
import com.shanjupay.transaction.api.dto.PlatformChannelDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author whb
 * @create 2021-12-06 13:03
 * @desciption 平台支付参数配置Controller
 */
@Api(value = "商户平台-交易服务渠道和支付参数相关",tags = "商户平台-交易服务渠道和支付参数",description = "商户平台-交易服务渠道和支付参数相关")
@RestController
@Slf4j
public class PlatformParamController {
    @Reference
    private PayChannelService payChannelService;

    /**
     * 查询平台的服务类型
     * @return
     */
    @ApiOperation("获取平台服务类型集合")
    @RequestMapping(value = "/my/platform-channels",method = RequestMethod.GET)
    public List<PlatformChannelDTO> queryPlatformChannel(){
        return payChannelService.queryPlatformChannelList();
    }

    /**
     * 为指定应用绑定一个服务类型
     * @param appId 应用app的id
     * @param platformChannelCodes 平台服务类型code
     */
    @ApiOperation("给指定app应用绑定平台服务类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId",value = "应用app的id",required = true,dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "platformChannelCodes",value = "平台服务类型code",required = true,dataType = "String",paramType = "query")
    })
    @RequestMapping(value = "/my/apps/{appId}/platform-channels",method = RequestMethod.POST)
    public void bindPlatformForApp(@PathVariable("appId") String appId, @RequestParam("platformChannelCodes") String platformChannelCodes){
        payChannelService.bindPlatformChannelForApp(appId, platformChannelCodes);
    }

    /**
     * 查询app应用绑定服务类型的状态
     * @param appId app应用id
     * @param platformChannelCodes 平台服务类型code
     * @return
     */
    @ApiOperation("查询应用是否绑定了某个服务类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId",value = "应用app的id",required = true,dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "platformChannelCodes",value = "平台服务类型code",required = true,dataType = "String",paramType = "path")
    })
    @RequestMapping(value = "/my/merchants/apps/platformchannels",method = RequestMethod.GET)
    public int queryAppBindPlatformChannel(@RequestParam String appId, @RequestParam String platformChannelCodes){
        return payChannelService.queryAppBindPlatformChannel(appId, platformChannelCodes);
    }

    /**
     * 根据平台服务类型code查询第三方支付渠道
     * @param platformChannelCode 平台服务类型code
     * @return 第三方支付渠道集合
     */
    @ApiOperation("根据平台服务类型查询第三方支付渠道")
    @ApiImplicitParam(name = "platformChannelCode",value = "平台服务类型code",required = true,dataType = "String",paramType = "path")
    @RequestMapping(value = "/my/pay-channels/platform-channel/{platformChannelCode}",method = RequestMethod.GET)
    List<PayChannelDTO> queryPayChannelByPlatformChannel(@PathVariable("platformChannelCode") String platformChannelCode){
        return payChannelService.queryPayChannelByPlatformChannel(platformChannelCode);
    }

    /**
     * 商户配置支付渠道参数
     * @param payChannelParamDTO 参数对象
     */
    @ApiOperation("商户配置支付渠道参数")
    @ApiImplicitParam(name = "payChannelParamDTO", value = "支付渠道参数", required = true, dataType = "PayChannelParamDTO", paramType = "body")
    @RequestMapping(value = "/my/pay-channel-params",method = {RequestMethod.POST,RequestMethod.PUT})
    void createPayChannelParam(@RequestBody PayChannelParamDTO payChannelParamDTO){
        if(payChannelParamDTO == null || payChannelParamDTO.getChannelName() == null){
            throw new BusinessException(CommonErrorCode.E_300009);
        }
        //商户id
        Long merchantId = SecurityUtil.getMerchantId();
        payChannelParamDTO.setMerchantId(merchantId);
        payChannelService.savePayChannelParam(payChannelParamDTO);
    }

    /**
     * 根据app应用和平台服务类型获取支付渠道参数列表
     * @param appId app应用appId
     * @param platformChannel 平台服务类型code
     * @return
     */
    @ApiOperation("根据应用和服务类型获取支付渠道参数列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "应用appId",name = "appId",dataType = "String",paramType = "path"),
            @ApiImplicitParam(value = "服务类型代码",name = "platformChannel",dataType = "String",paramType = "path")
    })
    @GetMapping(value = "/platform/pay/channel/params/apps/{appId}/platform-channels/{platformChannel}")
    public  List<PayChannelParamDTO> queryPayChannelParam(@PathVariable("appId")String appId,@PathVariable("platformChannel")String platformChannel){
        return payChannelService.queryPayChannelParamByAppAndPlatform(appId,platformChannel);
    }

    /**
     * 根据app应用appid、平台服务类型code和支付渠道code查询对应的支付渠道参数
     * @param appId app应用appid
     * @param platformChannel 平台服务类型code
     * @param payChannel 支付渠道code
     * @return
     */
    @ApiOperation("根据应用和服务类型和支付渠道获取单个支付渠道参数")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "应用id",name = "appId",dataType = "String",paramType = "path"),
            @ApiImplicitParam(value = "服务类型代码",name = "platformChannel",dataType = "String",paramType = "path"),
            @ApiImplicitParam(value = "支付渠道代码",name = "payChannel",dataType = "String",paramType = "path")
    })
    @GetMapping(value = "/platform/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}/pay-channels/{payChannel}")
    public  PayChannelParamDTO queryPayChannelParam(@PathVariable("appId")String appId,@PathVariable("platformChannel")String platformChannel,@PathVariable("payChannel") String payChannel){
        return payChannelService.queryParamByAppPlatformAndPayChannel(appId,platformChannel,payChannel);
    }
}
